package com.bss.dao;

import java.sql.*;
//import com.mysql.jdbc.*;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

import com.bss.BackupThread;
import com.bss.framework.FrontController;

public class ConnectionDao {
	
	public static final String PASSWORD = "nopass";
	public static final String DATABASE = "bss"; 
	private static Connection connection;
	private static Thread backupThread = null;
	
	public static java.sql.Connection getConnection()
	{
		
	    java.sql.Connection conn = null;
	    String url = "jdbc:mysql://localhost:3306/";
	    String dbName = DATABASE;
	    String driver = "com.mysql.jdbc.Driver";
	    String userName = "root"; 
	    String password = PASSWORD;
	    try {
	      Class.forName(driver).newInstance();
	      conn = DriverManager.getConnection(url+dbName,userName,password);
	      com.bss.framework.FrontController.debug("Connected to the database; Connection Type: Plain");

	      return conn;
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	    
	    return null;
	}
	
	private static Connection getJNDIConnection() throws Exception {
		Object obj = null;
		try {
			//get the initial context
	        InitialContext initCtx = new InitialContext();
	        
	        //look up for the database
	        obj = initCtx.lookup("java:comp/env/jdbc/bss");
	        
	        // get the data source
	        DataSource ds = (DataSource) obj;		    
		    Connection conn = ds.getConnection(); 
		    com.bss.framework.FrontController.debug("Connected to the database; Connection Type: JNDI");
	        return conn;
		}
	    catch (Exception ex)   {
	            com.bss.framework.FrontController.error(ex.getMessage());
	    }	
	    return null;
	}

	public static java.sql.Connection getConnection1()
	throws Exception {
				
		//if(connection == null)
		//	connection = getJNDIConnection();
		if(connection == null)
			connection = getConnection();
		if(connection != null)
			connection.setAutoCommit(true);
		
		//System.out.println("Connection: "+connection);
		
		new CheckInOutDao();
//		if(backupThread==null || !backupThread.isAlive()) {
//			backupThread = new BackupThread();
//			backupThread.start();
//			FrontController.debug("Database Backup Thread Started...");
//		}
		
		return connection;
	}	

	public static int closeConnection(java.sql.Connection conn)
	{
		try{
			// This is not recommended
			//conn.close();
			//com.bss.framework.FrontController.trace("Disconnected from database");
		}
		catch (Exception e)
		{
		
		}
		return 1;
	}
	
	public static java.sql.Connection getDeveloperConnection()
	{
		
	    java.sql.Connection conn = null;
	    String url = "jdbc:mysql://192.168.56.102:3306/";
	    String dbName = DATABASE;
	    String driver = "com.mysql.jdbc.Driver";
	    String userName = "root"; 
	    String password = PASSWORD;
	    try {
	      Class.forName(driver).newInstance();
	      conn = DriverManager.getConnection(url+dbName,userName,password);
	      com.bss.framework.FrontController.trace("Connected to the database. Connection Type: Developer");

	      return conn;
	      
	    } catch (Exception e) {
	      e.printStackTrace();
	    }
	    
	    return null;
	    
		
	}
	
	/*
	 
	  <?xml version="1.0" encoding="UTF-8"?>
		<Context path="/BSS">
		  <Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="20" 
		  			maxIdle="10" maxWait="-1" name="jdbc/bss" 
		  			type="javax.sql.DataSource" url="jdbc:mysql://192.168.56.102:3306/bss" username="root" password="nopass" 
		  			/>
		</Context>

	 
	 */
	

}
